package cn.test.service.impl;

import cn.test.MainMenu;
import cn.test.dao.CustInfoDao;
import cn.test.dao.impl.CustInfoDaoImpl;
import cn.test.entity.CusInfo;
import cn.test.service.CusInfoSer;

import java.util.List;
import java.util.Scanner;

public class CusInfoSerImpl implements CusInfoSer {
    CustInfoDao cus = new CustInfoDaoImpl();

    @Override
    public void optionFunction() {
        Scanner scanner = new Scanner(System.in);
        String flag = null;
        do {
            this.showMenu();
            int opt = scanner.nextInt();
            switch (opt){
                case 1:
                    CusInfo cusInfo = new CusInfo();
                    System.out.print("姓名:");
                    cusInfo.setName(scanner.next());
                    System.out.print("性别:");
                    cusInfo.setGender(scanner.next());
                    System.out.print("年龄:");
                    cusInfo.setAge(scanner.nextInt());
                    System.out.print("电话:");
                    cusInfo.setPhone(scanner.next());
                    System.out.print("邮箱:");
                    cusInfo.setMail(scanner.next());
                    cus.insert(cusInfo);
                    break;
                case 2:
                    CusInfo mod = new CusInfo();
                    System.out.print("请输入用户需要修改的UID:");
                    mod.setUid(scanner.nextInt());
                    System.out.println("请重新输入以下信息：");
                    System.out.print("姓名:");
                    mod.setName(scanner.next());
                    System.out.print("性别:");
                    mod.setGender(scanner.next());
                    System.out.print("年龄:");
                    mod.setAge(scanner.nextInt());
                    System.out.print("电话:");
                    mod.setPhone(scanner.next());
                    System.out.print("邮箱:");
                    mod.setMail(scanner.next());
                    cus.mod(mod);
                    break;
                case 3:
                    CusInfo uid = new CusInfo();
                    System.out.print("请输入删除的序号：");
                    uid.setUid(scanner.nextInt());
                    cus.del(uid);
                    break;
                case 4:
                    List<CusInfo> cusInfos = cus.selectAll();
                    System.out.println("--- 客户列表 ---");
                    if (cusInfos.size()==0) {
                        System.out.println("- 暂无数据 -");
                    }else {
                        for (CusInfo info : cusInfos) {
                            System.out.println(info.toString());
                        }
                    }
                    break;
                case 5:
                    System.out.println("- 程序结束 -");
                    return;
            }
            System.out.println("是否继续y/n");
            flag = scanner.next();
        }while (flag.equalsIgnoreCase("y"));
        System.out.println("- 程序结束 -");
    }

    public void showMenu(){
        System.out.println("----- 客户信息管理软件 -----");
        System.out.println("\t 1.添 加 客 户");
        System.out.println("\t 2.修 改 客 户");
        System.out.println("\t 3.删 除 客 户");
        System.out.println("\t 4.客 户 列 表");
        System.out.println("\t 5.退 出");
        System.out.print("请选择功能:");
    }
}
